package com.java.base.exercise.current;

import org.apache.poi.ss.formula.functions.T;

import java.util.Iterator;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.LockSupport;

/**
 * @Author RenPu
 * @Date 2022/2/15 15:14
 * @Version 1.0
 * @Description: 自定实现AQS核心链路
 **/
public class AQSdemo {

    //同步资源状态（计数状态的变更..CountDownLatch.. CyclicBarrier的实现.）
    private volatile AtomicInteger state=new AtomicInteger(0);

    //记录当前锁的拥有者
     volatile AtomicReference<Thread> owen=new AtomicReference<>();

    //等待线程的存储容器
     volatile LinkedBlockingQueue <Thread>waitThreadQueue=new LinkedBlockingQueue<Thread>();


/*-------------------------------------------AQS独享锁的实现-----------------------------------------*/
    //获取锁的方法
    public void acquire() {
        boolean mark=true;
        while (!tryAcquire()){
            if(mark){
                waitThreadQueue.offer(Thread.currentThread());
                mark=false;
            }else {
                LockSupport.park();
            }
        }
        waitThreadQueue.remove(Thread.currentThread());
    }

    //释放锁的方法
    public void release() {
        if(tryRelease()){
            Iterator<Thread> iterator = waitThreadQueue.iterator();
            while (iterator.hasNext()){
                Thread next = iterator.next();
                owen.compareAndSet(Thread.currentThread(),null);
                LockSupport.unpark(next);  //唤醒线程继续，获取锁
            }

        }
    }






/*-----------------------------------------AQS共享锁的实现---------------------------------------*/

    // 判断量够不够
    public void acquireShared() {
      boolean mark=true;
      while (tryAcquireShared()<0){
          if(mark){
              waitThreadQueue.offer(Thread.currentThread());
              mark=false;
          }else {
              LockSupport.park();
          }
      }
      waitThreadQueue.remove(Thread.currentThread());
    }


    public void releaseShared() {
        if(tryReleaseShared()){
            Iterator<Thread> iterator = waitThreadQueue.iterator();
            while (iterator.hasNext()){
                Thread next = iterator.next();
                LockSupport.unpark(next);
            }

        }

    }




    /**
     * 独享锁----尝试获取锁方法
     * @return
     */
    public boolean tryAcquire() {
        throw new UnsupportedOperationException();
    }

    /**
     * 独享锁----释放资源
     * @return
     */
    public boolean tryRelease() {
        throw new UnsupportedOperationException();
    }

    /**
     * 共享锁------多个获取
     * @return
     */
    public int tryAcquireShared() {
        throw new UnsupportedOperationException();
    }

    /**
     * 共享锁-----尝试释放多个，资源
     * @return
     */
    public boolean tryReleaseShared() {
        throw new UnsupportedOperationException();
    }


    public AtomicInteger getState() {
        return state;
    }

    public void setState(AtomicInteger state) {
        this.state = state;
    }

}
